//
//  SignInSelectViewController.m
//  Marketing
//
//  Created by User on 16/5/20.
//  Copyright © 2016年 Hanen 3G 01. All rights reserved.
//

#import "SignInSelectViewController.h"
#import "MapClientViewController.h"

#define CELLHEIGHT4 60.0f
@interface SignInSelectViewController ()<MapClientViewControllerDelegate,UIActionSheetDelegate,UIImagePickerControllerDelegate,UINavigationControllerDelegate>
{
    CGFloat  longt;
    CGFloat   lati;
    UIButton *addButt;
    UIButton *selectAddress;
    NSString *PicString;
    UILabel *addressLab;
    CLLocationCoordinate2D lonlat;
}
@end

@implementation SignInSelectViewController

- (void)viewWillDisappear:(BOOL)animated
{
    self.tabBarController.hidesBottomBarWhenPushed = NO;
}
- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.hidesBottomBarWhenPushed = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.navigationItem.titleView = [ViewTool getNavigitionTitleLabel:@"签到"];
    self.view.backgroundColor = graySectionColor;
    self.navigationItem.leftBarButtonItem=[ViewTool getBarButtonItemWithTarget:self WithAction:@selector(goToBack)];
    
    NSLog( @"8888888_______-------   %d",self.butttag);
    
    [self creatUI];
}

- (void)creatUI
{
    [self addAreaView:CGRectMake(0, 0, KSCreenW, 20)];
    UIView *bgView = [[UIView alloc] initWithFrame:CGRectMake(0, 20, KSCreenW, CELLHEIGHT4*2)];
    bgView.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:bgView];
    
    UILabel *address = [self getLabeltitle:@"签到地点" font:nil color:blackFontColor frame:CGRectMake(LEFTWIDTH, 40, 80, 20)];
    
    addressLab = [self getLabeltitle:@"请选择签到地点" font:nil color:grayFontColor frame:CGRectMake(address.maxX+10, address.y, KSCreenW - 40-80, 20)];
    [ViewTool setLableFont14:addressLab];
    
    [self.view addSubview:addressLab];
    
    selectAddress = [[UIButton alloc] initWithFrame:CGRectMake(address.maxX+10, address.y, KSCreenW - 40-80, 20)];
    selectAddress.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    selectAddress.contentEdgeInsets = UIEdgeInsetsMake(0, 0, 0, 0);
    
    [selectAddress setTitleColor:grayFontColor forState:UIControlStateNormal];
    selectAddress.titleLabel.font = [UIView getFontWithSize:14];
    [selectAddress addTarget:self action:@selector(selectedaddress) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:selectAddress];
    UIButton *cornorButt = [[UIButton alloc] initWithFrame:CGRectMake(KSCreenW-40, 30, 40, 40)];
    [cornorButt setImage:[UIImage imageNamed:@"HY_list_icon_arrow"] forState:UIControlStateNormal];
    [cornorButt addTarget:self action:@selector(selectedaddress) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:cornorButt];
    
    UILabel *time = [self getLabeltitle:@"签到时间" font:nil color:blackFontColor frame:CGRectMake(LEFTWIDTH, address.maxY+40, 80, 20)];
    
    NSDateFormatter *form = [[NSDateFormatter alloc] init];
    [form setDateFormat:@"hh:mm"];
    NSString *date = [form stringFromDate:[NSDate date]];
    
    UILabel *signlab = [self getLabeltitle:date font:nil color:grayFontColor frame:CGRectMake(time.maxX+10, time.y, 100, 20)];
    
    UIView *line = [[UIView alloc] initWithFrame:CGRectMake(LEFTWIDTH, address.maxY +20, KSCreenW, 1)];
    line.backgroundColor = grayLineColor;
    [self.view addSubview:line];
    
    [self addAreaView:CGRectMake(0, bgView.maxY, KSCreenW, 20)];
    
    UIView *bgView1 = [[UIView alloc] initWithFrame:CGRectMake(0, time.maxY+40, KSCreenW, CELLHEIGHT4*2)];
    bgView1.backgroundColor = [UIColor whiteColor];
    
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(0, bgView1.height - 1, KSCreenW, 1)];
    line1.backgroundColor = grayLineColor;
    [bgView1 addSubview:line1];
    
    [self.view addSubview:bgView1];
    UILabel *addPic = [self getLabeltitle:@"添加照片" font:nil color:blackFontColor frame:CGRectMake(LEFTWIDTH, time.maxY+90, 100, 20)];
    addButt = [[UIButton alloc] initWithFrame:CGRectMake(addPic.maxX+10, 10, 100, 100)];
    [addButt setImage:[UIImage imageNamed:@"HY_list_btn_add_pic_normal"] forState:UIControlStateNormal];
    [addButt addTarget:self action:@selector(clickToChangeHeaderImage:) forControlEvents:UIControlEventTouchUpInside];
    [bgView1 addSubview:addButt];
    
    UIView *DownbgView = [[UIView alloc] initWithFrame:CGRectMake(0, KSCreenH - 80 - 64, KSCreenH, 80)];
    DownbgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:DownbgView];
    
    UIButton *CommitBtn = [[UIButton alloc] initWithFrame:CGRectMake(LEFTWIDTH, 10, KSCreenW-2*LEFTWIDTH, 60)];
    [CommitBtn setBackgroundImage:[UIImage imageNamed:@"HY_btn_red_normal"] forState:UIControlStateNormal];
    [CommitBtn setTitleColor:[UIColor whiteColor]forState:UIControlStateNormal];
   
    [ViewTool setLableFont14:CommitBtn.titleLabel];
    [CommitBtn setTitle:@"确认提交" forState:UIControlStateNormal];

    //    [CommitBtn setBackgroundImage:[UIImage imageNamed:@"HY_btn_red_normal"] forState:UIControlStateNormal];
    
    [CommitBtn addTarget:self action:@selector(CommitClick) forControlEvents:UIControlEventTouchUpInside];
    [DownbgView addSubview:CommitBtn];
    
    UIView *linev1 = [ViewTool getLineViewWith:CGRectMake(0, 0, KSCreenW, 1) withBackgroudColor:grayLineColor];
    [DownbgView addSubview:linev1];
    
    [self.view addSubview:address];
    [self.view addSubview:signlab];
    [self.view addSubview:time];
    [self.view addSubview:addPic];
}

- (void)CommitClick
{
    
    NSString *address = addressLab.text;
    if ([address isEqualToString:@"请选择签到地点"]) {
        [SVProgressHUD showInfoWithStatus:@"请选择签到地点"];
        return;
    }
    if (PicString.length == 0) {
        [SVProgressHUD showInfoWithStatus:@"请选择照片"];
        return;
    }
    NSDateFormatter *form = [[NSDateFormatter alloc] init];
    [form setDateFormat:@"YYYY-MM-dd hh:mm:ss"];
    NSString *date = [form stringFromDate:[NSDate date]];
    NSString *lat = [NSString stringWithFormat:@"%f",lonlat.latitude];
    NSString *lon = [NSString stringWithFormat:@"%f",lonlat.longitude];
    
    NSDictionary * dic1 =@{@"token":TOKEN,@"uid":@(UID),@"signinaddress":address,@"signinlon":lon,@"signinlat":lat,@"signintime":date,@"vid":@(self.model.visitId)};
    
    NSDictionary *dic2  =@{@"token":TOKEN,@"uid":@(UID),@"signoutaddress":address,@"signoutlon":lon,@"signoutlat":lat,@"signouttime":date,@"id":@(self.model.visitId)};
    
    NSString *urlstr = @"";
    NSMutableDictionary *dic;;
    //签到
    if (_butttag == 1) {
        dic = [NSMutableDictionary dictionaryWithDictionary:dic1];
        urlstr = SIGN_IN_VISIT_URL;
    }//签退
    if (_butttag == 2) {
      dic = [NSMutableDictionary dictionaryWithDictionary:dic2];
        urlstr = SIGN_OUT_VISIT_URL;
        
    }
    if (PicString.length != 0) {
        if (_butttag == 1) {
            [dic setObject:PicString forKey:@"signinimg"];
        }
        if (_butttag == 2) {
            [dic setObject:PicString forKey:@"signoutimg"];
        }
    }

    
    [DataTool postWithUrl:urlstr parameters:dic success:^(id data) {
        NSDictionary *jsonDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        int code = [[jsonDic objectForKey:@"code"] intValue];
        if(code != 100)
        {
            NSString * message = [jsonDic objectForKey:@"message"];
            [self.view makeToast:message];
        }
        else{
        [self.navigationController popViewControllerAnimated:NO];
        }
    } fail:^(NSError *error) {
        [self.view makeToast:error.localizedDescription];
    }];
    
}

#pragma mark------修改头像
- (void)clickToChangeHeaderImage:(UIButton *)btn{
    if(IOS7){
        UIActionSheet * actionSheet = [[UIActionSheet alloc] initWithTitle:nil delegate:self cancelButtonTitle:@"取消" destructiveButtonTitle:nil otherButtonTitles:@"从相册选择",@"打开相机", nil];
        [actionSheet showInView:self.view];
    }else{
        UIAlertController * alertionControll = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        UIAlertAction * action1 = [UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self takePhotoFromLibrary];
        }];
        UIAlertAction * action2 = [UIAlertAction actionWithTitle:@"打开相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self takePhotoFromCamera];
        }];
        UIAlertAction * action3 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [alertionControll addAction:action1];
        [alertionControll addAction:action2];
        [alertionControll addAction:action3];
        if(alertionControll != nil){
            [self presentViewController:alertionControll animated:YES completion:nil];
        }
    }
}
#pragma mark
#pragma mark --actionSheet delegate
- (void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        [self takePhotoFromLibrary];
    }else if (buttonIndex == 1){
        [self takePhotoFromCamera];
    }
}
- (void)takePhotoFromLibrary{
    UIImagePickerController *imagePick = [[UIImagePickerController alloc] init];
    imagePick.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePick.delegate = self;
    imagePick.allowsEditing = YES;
    [self presentViewController:imagePick animated:YES completion:nil];
}
- (void)takePhotoFromCamera{
    UIImagePickerController *imagePic = [[UIImagePickerController alloc]init];
    imagePic.sourceType = UIImagePickerControllerSourceTypeCamera;
    imagePic.delegate = self;
    imagePic.allowsEditing = YES;
    [self presentViewController:imagePic animated:YES completion:nil];
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    [picker dismissViewControllerAnimated:YES completion:nil];
    UIImage *image = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    [addButt setImage:image forState:UIControlStateNormal];
    NSData *imageData = UIImageJPEGRepresentation(image, 0.5);
    NSString *str = [imageData base64Encoding];
    PicString = str;
}

- (void)actionSheetCancel:(UIActionSheet *)actionSheet
{
    [actionSheet dismissWithClickedButtonIndex:2 animated:YES];
}

#pragma -----跳转地图
- (void)selectedaddress
{
    MapClientViewController * mapVC = [[MapClientViewController alloc] init];
    
    if (longt == 0&&lati == 0) {
        mapVC.result = 1;
    }else{
        mapVC.locationLongide = [NSString stringWithFormat:@"%f",longt];
        mapVC.locationLatitude =  [NSString stringWithFormat:@"%f",lati];
    }
    mapVC.delegate = self;
    [self.navigationController pushViewController:mapVC animated:YES];
    
}

- (void)getClickPlaceName:(NSString *)PlaceString withCoordinat2D:(CLLocationCoordinate2D)pt{
    addressLab.text = PlaceString;
    

    
    lonlat = pt;
}

- (void)addAreaView:(CGRect)frame
{
    UIView *view = [[UIView alloc] initWithFrame:frame];
    view.backgroundColor = graySectionColor;
    [self.view addSubview:view];
    
    UIView *line1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KSCreenW, 1)];
    line1.backgroundColor = grayLineColor;
    [view addSubview:line1];
    
    UIView *line2 = [[UIView alloc] initWithFrame:CGRectMake(0, view.height - 1, KSCreenW, 1)];
    line2.backgroundColor = grayLineColor;
    [view addSubview:line2];
    
}

- (UILabel *)getLabeltitle:(NSString *)title font:(UIFont *)font color:(UIColor *)color frame:(CGRect)rect
{
    UILabel *lab = [[UILabel alloc] initWithFrame:rect];
    lab.font = font;
    lab.textColor = color;
    lab.text = title;
    lab.textAlignment = NSTextAlignmentLeft;
    [ViewTool setLableFont14:lab];
    
    return lab;
}

- (void)goToBack{
    [self.navigationController popViewControllerAnimated:NO];
}
@end
